package com.bjpowernode.threadpool.shutdown;

import com.bjpowernode.util.ThreadUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class TestService {

    @Resource
    ThreadPoolExecutor threadPoolExecutor2;

    /**
     * 下订单
     */
    public void doOrder() {
        for (int i = 0; i < 10; i++) {
            threadPoolExecutor2.execute(() -> {
                ThreadUtils.sleepMilliSeconds(200);
                System.out.println("线程执行任务 1 ......");
            });
        }
        //关闭线程池
        threadPoolExecutor2.shutdown();
        try {
            while (!threadPoolExecutor2.awaitTermination(100, TimeUnit.MILLISECONDS)) {
                System.out.println("线程池还在运行 1 ......");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程池已经完全关闭 1 ......");
    }

    /**
     * 发短信
     */
    public void doSMS() {
        for (int i = 0; i < 5; i++) {
            System.out.println("doSomeThing2 。。。。。。");
            threadPoolExecutor2.execute(() -> {
                System.out.println(" 2222222222222222222 ");
                ThreadUtils.sleepMilliSeconds(200);
                System.out.println("线程执行任务 2 ......");
            });
        }
        //关闭线程池
        threadPoolExecutor2.shutdown();
        try {
            while (!threadPoolExecutor2.awaitTermination(100, TimeUnit.MILLISECONDS)) {
                System.out.println("线程池还在运行 2 ......");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程池已经完全关闭 2 ......");
    }
}
